package local.blueheart.data.springdatademo.task;

import local.blueheart.data.springdatademo.annotation.TaskWorker;
import local.blueheart.data.springdatademo.domain.config.ApplicationDO;
import local.blueheart.data.springdatademo.service.XmlConfigService;
import local.blueheart.data.springdatademo.service.impl.ArchiveIndexDataServiceImpl;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.List;
import java.util.concurrent.Executor;

@TaskWorker("索引归档任务")
public class IndexArchivedTask implements Job, ApplicationContextAware {

    private static final Logger LOGGER = LoggerFactory.getLogger(IndexArchivedTask.class);

    @Autowired
    private XmlConfigService xmlConfigService;

    private ApplicationContext applicationContext;

    @Autowired
    private Executor  executor;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        LOGGER.info("======start=========");
        List<ApplicationDO> applicationDOList = xmlConfigService.getAllApplication();
        for(ApplicationDO applicationDO:applicationDOList){
            ArchiveIndexDataServiceImpl archiveIndexDataService = (ArchiveIndexDataServiceImpl) applicationContext.getBean("archiveIndexDataService");
            archiveIndexDataService.setApplicationDO(applicationDO);
            executor.execute(archiveIndexDataService);
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
